package yx.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.model.BsConfig;
import yx.service.ConfigServiceInt;
import yx.service.TreeServiceInt;
import yx.vo.JsonVO;
import yx.vo.TreeVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "treeAction")
public class TreeAction extends BaseAction implements ModelDriven<TreeVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TreeAction.class);

	private TreeVO tree = new TreeVO();

	@Override
	public TreeVO getModel() {
		return tree;
	}

	private TreeServiceInt treeService;
	private ConfigServiceInt configService;

	public TreeServiceInt getTreeService() {
		return treeService;
	}

	@Autowired
	public void setTreeService(TreeServiceInt treeService) {
		this.treeService = treeService;
	}

	public ConfigServiceInt getConfigService() {
		return configService;
	}

	@Autowired
	public void setConfigService(ConfigServiceInt configService) {
		this.configService = configService;
	}

	public void addTreeNode() {
		JsonVO j = new JsonVO();
		try {
			TreeVO u = treeService.save(tree);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeTreeNode() {
		List<BsConfig> refList = configService.getRefTable();
		if (refList != null) {
			String tableName = "";
			String fieldName = "";
			for (BsConfig t : refList) {
				if (t.getType().equals(tree.getType())) {
					tableName = t.getTableName();
					fieldName = t.getFieldName();
					break;
				}
			}
			if ("".equals(tableName) && "".equals(fieldName)) {
				// 配置表中对应的类型为空，可以删除
				treeService.removeById(tree.getId().toString());
				JsonVO j = new JsonVO();
				j.setSuccess(true);
				j.setMsg("删除成功！");
				super.writeJson(j);
			} else if (!"".equals(tableName) && "".equals(fieldName)) {
				// 配置表配置有问题
				JsonVO j = new JsonVO();
				j.setSuccess(false);
				j.setMsg("配置表的配置存在问题，请联系管理员！");
				super.writeJson(j);
			} else if ("".equals(tableName) && !"".equals(fieldName)) {
				// 配置表配置有问题
				JsonVO j = new JsonVO();
				j.setSuccess(false);
				j.setMsg("配置表的配置存在问题，请联系管理员！");
				super.writeJson(j);
			} else {
				// 配置表配置正确
				Boolean flag = configService.isRefTable(tree.getId(), tableName, fieldName);
				if (flag) {
					JsonVO j = new JsonVO();
					j.setSuccess(false);
					j.setMsg("数据被引用，不允许删除！");
					super.writeJson(j);
				} else {
					treeService.removeById(tree.getId().toString());
					JsonVO j = new JsonVO();
					j.setSuccess(true);
					j.setMsg("删除成功！");
					super.writeJson(j);
				}
			}
		} else {
			// 配置表为空，可以删除
			treeService.removeById(tree.getId().toString());
			JsonVO j = new JsonVO();
			j.setSuccess(true);
			j.setMsg("删除成功！");
			super.writeJson(j);
		}
	}

	/**
	 * 异步获取树节点
	 */
	public void queryTreeNode() {
		super.writeJson(treeService.getTreeNode(tree.getId().toString()));
	}

	/**
	 * 同步获取树节点
	 */
	public void queryAllTreeNode() {
		super.writeJson(treeService.getAllTreeNode(tree));
	}

}
